package com.avic.rong.activity.no;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.widget.Toast;

import com.avic.rong.MainActivity;
import com.avic.rong.MyApplication;
import com.avic.rong.R;
import com.avic.rong.bean.HomePageEvent;
import com.avic.rong.bean.MinePageEvent;
import com.example.minato.minastore.utils.AppUtils;
import com.github.lzyzsd.jsbridge.BridgeHandler;
import com.github.lzyzsd.jsbridge.BridgeWebView;
import com.github.lzyzsd.jsbridge.CallBackFunction;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;

public class H5Activity extends AppCompatActivity {


    public static String ARG_URL = "arg_url";

    @BindView(R.id.webview)
    BridgeWebView webView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_h5);
        ButterKnife.bind(this);

        String url = null;
        if (getIntent() != null){
            url = getIntent().getStringExtra(ARG_URL);
        }

        webView.loadUrl(url);

        webView.registerHandler("pushVc", new BridgeHandler() {
            @Override
            public void handler(String data, CallBackFunction function) {

                data = data.replace("\\","");
                JsonObject jsonObject = (JsonObject)new JsonParser().parse(data);
                String currentUrl = jsonObject.get("url").getAsString();
                if (MyApplication.getInstance().userLoginBean != null) {
                    if (currentUrl.contains("?")){
                        currentUrl = currentUrl +"&userIdSign=" + MyApplication.getInstance().userLoginBean.getUserId();
                    }else{
                        currentUrl = currentUrl +"?userIdSign=" + MyApplication.getInstance().userLoginBean.getUserId();
                    }
                }
                if (currentUrl.contains("/cservice")){//在线客服
                    String tempUrl = currentUrl.replace("userIdSign","userId");
                    Intent intent = new Intent();
                    intent.setData(Uri.parse(tempUrl));
                    intent.setAction(Intent.ACTION_VIEW);
                    startActivity(intent);
//                    startActivity(IMActivity.queryIntent(H5Activity.this,currentUrl.replace("userIdSign","userId")));
                }
            }
        });

        webView.registerHandler("popVCReload", new BridgeHandler() {
            @Override
            public void handler(String data, CallBackFunction function) {
                finish();
            }
        });

        webView.registerHandler("goBack", new BridgeHandler() {
            @Override
            public void handler(String data, CallBackFunction function) {
                finish();
            }
        });

        //app/searchIndex页面的返回
        webView.registerHandler("homeClick", new BridgeHandler() {
            @Override
            public void handler(String data, CallBackFunction function) {
                finish();
            }
        });


        //反馈调用
        webView.registerHandler("appAlert", new BridgeHandler() {
            @Override
            public void handler(String data, CallBackFunction function) {
                System.out.println("----------------app" + data);
                JsonObject jsonObject = (JsonObject)new JsonParser().parse(data);
                String msgStr =  jsonObject.get("msg").getAsString();
                Toast toast = Toast.makeText(H5Activity.this, msgStr, Toast.LENGTH_SHORT);
                toast.setGravity(Gravity.CENTER, 0, 0);
                toast.show();
                finish();
            }
        });


        //首页
        webView.registerHandler("homeClick", new BridgeHandler() {
            @Override
            public void handler(String data, CallBackFunction function) {
                EventBus.getDefault().postSticky(new HomePageEvent());
                Intent intent = MainActivity.queryIntent(H5Activity.this);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
            }
        });

        //搜索
        webView.registerHandler("searchClick", new BridgeHandler() {
            @Override
            public void handler(String data, CallBackFunction function) {
                startActivity(SearchIndexActivity.queryIntent(H5Activity.this, AppUtils.BASE_URL + "app/searchIndex?type=0"));
            }
        });

        //我的融融
        webView.registerHandler("memberClick", new BridgeHandler() {
            @Override
            public void handler(String data, CallBackFunction function) {
                EventBus.getDefault().postSticky(new MinePageEvent());
                Intent intent = MainActivity.queryIntent(H5Activity.this);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
            }
        });



    }


    public static Intent queryIntent(Context context,String url){
        Intent intent = new Intent(context,H5Activity.class);
        intent.putExtra(ARG_URL,url);
        return intent;
    }
}
